package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.ResultCode;
import com.example.dao.StudentInfoDao;
import com.example.dao.TeacherInfoDao;
import com.example.dao.XueyuanInfoDao;
import com.example.dao.ZhuanyeInfoDao;
import com.example.entity.*;
import com.example.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentInfoService {
    @Resource
    private StudentInfoDao studentInfoDao;

    @Resource
    private XueyuanInfoDao xueyuanInfoDao;
    @Autowired
    private ZhuanyeInfoDao zhuanyeInfoDao;

    public Account login(String name, String password) {
        StudentInfo studentInfo =studentInfoDao.findByNameAndPassword(name,password);
        if(ObjectUtil.isEmpty(studentInfo)){
            throw new CustomException("-1","用户名、密码或角色错误");
        }
        return studentInfo;
    }

    public void register(StudentInfo studentInfo) {
        StudentInfo info = studentInfoDao.findByName(studentInfo.getName());
        if(ObjectUtil.isNotEmpty(info)){
            throw new CustomException(ResultCode.USER_EXIST_ERROR);
        }
        studentInfoDao.insertSelective(studentInfo);
    }

    public StudentInfo findById(long id) {
        return studentInfoDao.findById(id);
    }

    public void update(StudentInfo studentInfo) {
        studentInfoDao.updateByid(studentInfo.getId(),studentInfo.getName(),studentInfo.getSex(),studentInfo.getAge(),studentInfo.getCode(),studentInfo.getScore(),studentInfo.getXueyuanid(),
                studentInfo.getZhuanyeid(),studentInfo.getStatus(),studentInfo.getPassword());
    }

    public List<StudentInfo> findAll() {

        //把学院号改为学院名称
        //方式一,使用java代码逻辑
        List<StudentInfo> list = studentInfoDao.findAllStudent();
        for(StudentInfo studentInfo : list){
            if(ObjectUtil.isNotEmpty(studentInfo.getXueyuanid())) {
                XueyuanInfo xueyuanInfo = xueyuanInfoDao.selectById(studentInfo.getXueyuanid());
                studentInfo.setXueyuanName(xueyuanInfo.getName());
            }
            if(ObjectUtil.isNotEmpty(studentInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(studentInfo.getZhuanyeid());
                studentInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
        }
        return list;
        //方式二，使用sql关联查询,不好用，编辑的时候不会随时修改，还要修改update代码
        //return studentInfoDao.findAllJoinXueyuan();

    }

    public void add(StudentInfo studentInfo) {
        StudentInfo info=studentInfoDao.findByName(studentInfo.getName());
        if(ObjectUtil.isNotEmpty(info)){
            throw  new CustomException(ResultCode.USER_EXIST_ERROR);
        }
        //默认密码为123456
        if(ObjectUtil.isEmpty(studentInfo.getPassword())){
            studentInfo.setPassword("123456");

        }
        if(ObjectUtil.isEmpty(studentInfo.getLevel())) {
            studentInfo.setLevel(3);
        }
        studentInfoDao.insertSelective(studentInfo);
    }

    public void deleteById(Long id) {
        studentInfoDao.deleteById(id);
    }

    public StudentInfo getstatisricsInfo() {
        StudentInfo studentInfo = new StudentInfo();
        Integer biye,daiye,jiuye;
        biye=studentInfoDao.getBiye();
        daiye=studentInfoDao.getDaiye();
        jiuye=studentInfoDao.getJiuye();
        studentInfo.setBiye(biye);
        studentInfo.setDaiye(daiye);
        studentInfo.setJiuye(jiuye);
        return studentInfo;
    }
}
